#include "student.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

// 全局学生数组
static Student students[MAX_STUDENTS];
static int student_count = 0;

// 初始化学生管理器
void init_student_manager() {
    student_count = 0;
    memset(students, 0, sizeof(students));
    printf(" 学生管理器初始化完成\n");
}

// 验证学号格式
int validate_student_id(const char* id) {
    if (id == NULL || strlen(id) == 0 || strlen(id) >= MAX_ID_LEN) {
        return 0;
    }
    
    // 检查学号是否只包含数字和字母
    for (int i = 0; id[i] != '\0'; i++) {
        if (!isalnum(id[i])) {
            return 0;
        }
    }
    return 1;
}

// 检查学生是否存在
int is_student_exists(const char* id) {
    for (int i = 0; i < student_count; i++) {
        if (strcmp(students[i].id, id) == 0) {
            return 1;
        }
    }
    return 0;
}

// 添加学生
int add_student(const char* id, const char* name) {
    if (!validate_student_id(id)) {
        printf(" 学号格式无效: %s\n", id);
        return 0;
    }
    
    if (is_student_exists(id)) {
        printf(" 学号已存在: %s\n", id);
        return 0;
    }
    
    if (student_count >= MAX_STUDENTS) {
        printf(" 学生数量已达上限(%d)\n", MAX_STUDENTS);
        return 0;
    }
    
    if (name == NULL || strlen(name) == 0) {
        printf(" 学生姓名不能为空\n");
        return 0;
    }
    
    // 添加新学生
    Student* new_student = &students[student_count];
    strncpy(new_student->id, id, MAX_ID_LEN - 1);
    strncpy(new_student->name, name, MAX_NAME_LEN - 1);
    new_student->course_count = 0;
    new_student->total_credits = 0.0;
    
    student_count++;
    printf("添加学生成功: %s - %s\n", id, name);
    return 1;
}

// 删除学生
int delete_student(const char* id) {
    if (!validate_student_id(id)) {
        return 0;
    }
    
    for (int i = 0; i < student_count; i++) {
        if (strcmp(students[i].id, id) == 0) {
            // 将最后一个学生移到当前位置
            if (i < student_count - 1) {
                students[i] = students[student_count - 1];
            }
            student_count--;
            printf("删除学生成功: %s\n", id);
            return 1;
        }
    }
    
    printf("未找到学生: %s\n", id);
    return 0;
}

// 根据学号查找学生
Student* find_student_by_id(const char* id) {
    if (!validate_student_id(id)) {
        return NULL;
    }
    
    for (int i = 0; i < student_count; i++) {
        if (strcmp(students[i].id, id) == 0) {
            return &students[i];
        }
    }
    return NULL;
}

// 根据姓名查找学生
Student* find_student_by_name(const char* name) {
    if (name == NULL || strlen(name) == 0) {
        return NULL;
    }
    
    for (int i = 0; i < student_count; i++) {
        if (strcmp(students[i].name, name) == 0) {
            return &students[i];
        }
    }
    return NULL;
}

// 更新学生信息
int update_student_info(const char* id, const char* new_name) {
    Student* student = find_student_by_id(id);
    if (student == NULL) {
        printf("未找到学生: %s\n", id);
        return 0;
    }
    
    if (new_name == NULL || strlen(new_name) == 0) {
        printf("新姓名不能为空\n");
        return 0;
    }
    
    strncpy(student->name, new_name, MAX_NAME_LEN - 1);
    printf("更新学生信息成功: %s -> %s\n", id, new_name);
    return 1;
}

// 重新计算学生总学分
void recalculate_student_credits(Student* student) {
    if (student == NULL) return;
    
    student->total_credits = 0.0;
    // 注意：这里需要课程模块提供根据课程ID获取学分的功能
    // 在实际集成时会通过课程模块的接口获取
    printf("📊 重新计算学生 %s 的总学分\n", student->id);
}

// 获取所有学生
Student* get_all_students() {
    return students;
}

// 获取学生数量
int get_student_count() {
    return student_count;
}

// 列出所有学生
void list_all_students() {
    printf("\n=== 学生列表 (共%d人) ===\n", student_count);
    for (int i = 0; i < student_count; i++) {
        printf("%d. 学号: %s, 姓名: %s, 已选课程: %d, 总学分: %.1f\n", 
               i + 1, students[i].id, students[i].name, 
               students[i].course_count, students[i].total_credits);
    }
}

// 学生管理菜单
void student_management_menu() {
    int choice;
    char id[MAX_ID_LEN], name[MAX_NAME_LEN];
    
    while (1) {
        printf("\n=== 学生信息管理 ===\n");
        printf("1. 添加学生\n");
        printf("2. 删除学生\n");
        printf("3. 修改学生信息\n");
        printf("4. 按学号查找\n");
        printf("5. 按姓名查找\n");
        printf("6. 列出所有学生\n");
        printf("0. 返回主菜单\n");
        printf("请选择: ");
        
        scanf("%d", &choice);
        getchar(); // 清除换行符
        
        switch (choice) {
            case 1:
                printf("请输入学号: ");
                fgets(id, sizeof(id), stdin);
                id[strcspn(id, "\n")] = 0;
                
                printf("请输入姓名: ");
                fgets(name, sizeof(name), stdin);
                name[strcspn(name, "\n")] = 0;
                
                if (add_student(id, name)) {
                    log_operation("ADD_STUDENT", id, name);
                }
                break;
                
            case 2:
                printf("请输入要删除的学号: ");
                fgets(id, sizeof(id), stdin);
                id[strcspn(id, "\n")] = 0;
                
                if (delete_student(id)) {
                    log_operation("DELETE_STUDENT", id, "删除学生");
                }
                break;
                
            case 3:
                printf("请输入要修改的学号: ");
                fgets(id, sizeof(id), stdin);
                id[strcspn(id, "\n")] = 0;
                
                printf("请输入新姓名: ");
                fgets(name, sizeof(name), stdin);
                name[strcspn(name, "\n")] = 0;
                
                if (update_student_info(id, name)) {
                    log_operation("UPDATE_STUDENT", id, name);
                }
                break;
                
            case 4:
                printf("请输入要查找的学号: ");
                fgets(id, sizeof(id), stdin);
                id[strcspn(id, "\n")] = 0;
                
                Student* student = find_student_by_id(id);
                if (student) {
                    printf("找到学生: 学号: %s, 姓名: %s, 已选课程: %d, 总学分: %.1f\n",
                           student->id, student->name, student->course_count, student->total_credits);
                } else {
                    printf("未找到学号为 %s 的学生\n", id);
                }
                break;
                
            case 5:
                printf("请输入要查找的姓名: ");
                fgets(name, sizeof(name), stdin);
                name[strcspn(name, "\n")] = 0;
                
                student = find_student_by_name(name);
                if (student) {
                    printf("找到学生: 学号: %s, 姓名: %s, 已选课程: %d, 总学分: %.1f\n",
                           student->id, student->name, student->course_count, student->total_credits);
                } else {
                    printf("未找到姓名为 %s 的学生\n", name);
                }
                break;
                
            case 6:
                list_all_students();
                break;
                
            case 0:
                return;
                
            default:
                printf("无效选择，请重新输入\n");
        }
    }
}
